|
1
|
|
|
import { API_KEY } from '@env'; |
|
2
|
|
|
import config from '../config/config.json'; |
|
3
|
|
|
import storage from './storage'; |
|
4
|
|
|
|
|
5
|
|
|
const mapModel = { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Get all cities from API |
|
9
|
|
|
* @param API_KEY |
|
10
|
|
|
* @returns Promise<Object> |
|
11
|
|
|
*/ |
|
12
|
|
|
getCities: async function getCities(API_KEY: string): Promise<Object> { |
|
13
|
|
|
const token = await storage.readToken(); |
|
14
|
|
|
|
|
15
|
|
|
const response = await fetch(`${config.base_url}cities?api_key=${API_KEY}`, { |
|
16
|
|
|
method: 'GET', |
|
17
|
|
|
headers: { |
|
18
|
|
|
'x-access-token': token['token'] |
|
19
|
|
|
} |
|
20
|
|
|
}); |
|
21
|
|
|
|
|
22
|
|
|
const result = await response.json(); |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
return result; |
|
27
|
|
|
}, |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get closest city based on users location |
|
31
|
|
|
* @param API_KEY |
|
32
|
|
|
* @param userData |
|
33
|
|
|
* @returns Promise<object> |
|
34
|
|
|
*/ |
|
35
|
|
|
getClosestCity: async function getClosestCity(userData: object): Promise<Object> { |
|
36
|
|
|
const cities = await mapModel.getCities(API_KEY); |
|
37
|
|
|
// for (const city of Object.entries(cities['cities'])) { |
|
38
|
|
|
// console.log(city[1]['zones'][0]); |
|
39
|
|
|
// }; |
|
40
|
|
|
// const currentCity = { |
|
41
|
|
|
// 'name': |
|
42
|
|
|
// } |
|
43
|
|
|
|
|
44
|
|
|
return cities['cities'][1]; |
|
45
|
|
|
}, |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get zones for a given city |
|
49
|
|
|
* @param city |
|
50
|
|
|
* @returns string |
|
51
|
|
|
*/ |
|
52
|
|
|
getZones: function getZones(city: object): string[] { |
|
53
|
|
|
|
|
54
|
|
|
// All zones in current city |
|
55
|
|
|
const zones = city['zones']; |
|
56
|
|
|
|
|
57
|
|
|
// Zone colors based on zoneType |
|
58
|
|
|
const zoneColors = { |
|
59
|
|
|
parkingZone: 'rgba(0, 194, 0, 0.3)', |
|
60
|
|
|
noParkingZone: 'rgba(255, 0, 0, 0.3)', |
|
61
|
|
|
bonusParkingZone: 'rgba(245, 40, 145, 0.3)', |
|
62
|
|
|
chargingZone: 'rgba(255, 255, 5, 0.3)' |
|
63
|
|
|
}; |
|
64
|
|
|
|
|
65
|
|
|
// Array to be returned containing all zones and data |
|
66
|
|
|
const zoneMarkers = []; |
|
67
|
|
|
|
|
68
|
|
|
// Loop through all zones and append them to return object |
|
69
|
|
|
for (let i = 0; i < zones.length; i++) { |
|
70
|
|
|
const coordinates = city['zones'][i]['coordinates']; |
|
71
|
|
|
|
|
72
|
|
|
// console.log(city['zones'][i]['zoneType']); |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
const zone = { |
|
76
|
|
|
_id: city['zones'][i]['_id'], |
|
77
|
|
|
zoneType: city['zones'][i]['zoneType'], |
|
78
|
|
|
zoneColor: zoneColors[city['zones'][i]['zoneType']], |
|
79
|
|
|
coordinates: [] |
|
80
|
|
|
}; |
|
81
|
|
|
for (const LatLng of coordinates) { |
|
82
|
|
|
zone['coordinates'].push({latitude: LatLng[1], longitude: LatLng[0]}) |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
|
|
zoneMarkers.push(zone); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return zoneMarkers; |
|
89
|
|
|
}, |
|
90
|
|
|
|
|
91
|
|
|
calcDistance: function calcDistance(lat1, lon1, lat2, lon2) { |
|
92
|
|
|
function deg2rad(deg) { |
|
93
|
|
|
return deg * (Math.PI/180) |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
var R = 6371; // Radius of the earth in km |
|
97
|
|
|
var dLat = deg2rad(lat2-lat1); // deg2rad below |
|
98
|
|
|
var dLon = deg2rad(lon2-lon1); |
|
99
|
|
|
var a = |
|
100
|
|
|
Math.sin(dLat/2) * Math.sin(dLat/2) + |
|
101
|
|
|
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * |
|
102
|
|
|
Math.sin(dLon/2) * Math.sin(dLon/2) |
|
103
|
|
|
; |
|
104
|
|
|
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); |
|
105
|
|
|
var d = R * c; // Distance in km |
|
106
|
|
|
return Math.round(d); |
|
107
|
|
|
} |
|
108
|
|
|
}; |
|
109
|
|
|
|
|
110
|
|
|
export default mapModel; |